<?php
ini_set('date.timezone','Asia/Shanghai');
error_reporting(E_ERROR);

require_once "../lib/WxPay.Api.php";
require_once '../lib/WxPay.Notify.php';
require_once 'log.php';
require_once "../gobal.php";

//初始化日志
$logHandler= new CLogFileHandler("../logs/".date('Y-m-d').'.log');
$log = Log::Init($logHandler, 15);

class PayNotifyCallBack extends WxPayNotify
{
	//查询订单
	public function Queryorder($transaction_id)
	{
		$input = new WxPayOrderQuery();
		$input->SetTransaction_id($transaction_id);
		$result = WxPayApi::orderQuery($input);
		Log::DEBUG("query:" . json_encode($result));
		if(array_key_exists("return_code", $result)
			&& array_key_exists("result_code", $result)
			&& $result["return_code"] == "SUCCESS"
			&& $result["result_code"] == "SUCCESS")
		{
			return true;
		}
		return false;
	}

	//重写回调处理函数
	public function NotifyProcess($data, &$msg)
	{
		$data_str=json_encode($data);
		Log::DEBUG("call back:" . $data_str);

		$oid=$data["attach"];

		$notfiyOutput = array();

		$transaction_id=$data["transaction_id"];
		$appid=$data["appid"];
		$attach=$data["attach"];
		$bank_type=$data["bank_type"];
		$cash_fee=$data["cash_fee"];
		$fee_type=$data["fee_type"];
		$is_subscribe=$data["is_subscribe"];
		$mch_id=$data["mch_id"];
		$nonce_str=$data["nonce_str"];
		$openid=$data["openid"];
		$out_trade_no=$data["out_trade_no"];
		$result_code=$data["result_code"];
		$return_code=$data["return_code"];
		$sign=$data["sign"];
		$time_end=$data["time_end"];
		$total_fee=$data["total_fee"];
		$trade_type=$data["trade_type"];
		
		
		$sql="INTO `wxmp_payment`(`transaction_id`, `appid`, `attach`, `bank_type`, `cash_fee`, `fee_type`, `is_subscribe`, `mch_id`, `nonce_str`, `openid`, `out_trade_no`, `result_code`, `return_code`, `sign`, `time_end`, `total_fee`, `trade_type`, `order_sum_id`, `create_at`)";
		$sql.=" VALUES ('$transaction_id','$appid','$attach','$bank_type',$cash_fee,'$fee_type','$is_subscribe','$mch_id','$nonce_str','$openid','$out_trade_no','$result_code','$return_code','$sign','$time_end',$total_fee,'$trade_type',$oid,NOW() )";

        //$sql="into debug (info) values ('$sql' )";

		$eff=insert_row($sql);

		//$sql="order_sum set wxmp_transaction_id='$transaction_id',pay_stats=1 where id=$oid ";

		//$eff=update_row($sql);

		if(!array_key_exists("transaction_id", $data)){
			$msg = "输入参数不正确";
			return false;
		}
		//查询订单，判断订单真实性
		if(!$this->Queryorder($transaction_id)){
			$msg = "订单查询失败";
			return false;
		}
		
		$sql2="select * from order_sum_detail where id=$oid";

       $order_sums=fetch_rows($sql2);
       $order_sum=$order_sums[0];

		
		$content=htmlForOrderSum($order_sum);

		$title="訂單支付完成:".$order_sum["code"];


        $content.=$data_str;
	    sendEmailToAdmin($title,$content);

		return true;
	}
}

Log::DEBUG("begin notify");
$notify = new PayNotifyCallBack();
$notify->Handle(false);


/*

{"appid":"wx61e83a6dc6bc8eec","attach":"test","bank_type":"CFT","cash_fee":"1","fee_type":"CNY","is_subscribe":"Y","mch_id":"1339072501","nonce_str":"45kh7gng7qzr4mqoo1gacyc4fn2l1x8d","openid":"or-3ssrxhQG3B5C6Q1KtP64Kv2Ts","out_trade_no":"133907250120160609003511","result_code":"SUCCESS","return_code":"SUCCESS","sign":"F9471B05E5F27D5D30E2353B859D237A","time_end":"20160609003539","total_fee":"1","trade_type":"JSAPI","transaction_id":"4008732001201606096982129133"}

*/